package com.aplink.generic.xmlparser;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import android.text.TextUtils;

import com.aplink.generic.constant.MenuConstant;
import com.aplink.generic.pojo.controlspec.actionbar.ActionBarSpec;
import com.aplink.generic.pojo.controlspec.actionbar.SearchViewSpec;
import com.aplink.generic.pojo.controlspec.actionbar.SelectionSpec;
import com.aplink.generic.pojo.controlspec.menu.MenuItemSpec;
import com.aplink.generic.util.ResourcesHelper;

public class ActionBarParser {
	public static final String ACTION_BAR = "actionBar";
	public static final String BUTTON = "button";
	public static final String BUTTONS = "buttons";
	public static final String CONTENT_DATA = "contentData";
	public static final String DISPLAY_PROPERTY = "displayprop";
	public static final String FIELD = "field";
	public static final String ICON = "icon";
	public static final String ID = "id";
	public static final String IGNORE_INDEX = "ignoreIndex";
	public static final String IS_DISPLAY_HOME_AS_UP = "isDisplayHomeAsUp";
	public static final String IS_DISPLAY_SHOW_HOME_ENABLE = "isDisplayShowHomeEnabled";
	public static final String IS_DISPLAY_SHOW_TITLE_ENABLE = "isDisplayShowTitleEnabled";
	public static final String SEARCH_FOR = "searchFor";
	public static final String SEARCH_VIEW = "searchView";
	public static final String SELECTION = "selection";
	public static final String SUB_TITLE = "subTitle";
	public static final String TITLE = "title";
	public static final String VALUE_PROPERTY = "valueprop";

	public static ActionBarParser getInstance() {
		final ActionBarParser menuParser = new ActionBarParser();
		return menuParser;
	}

	public ActionBarSpec parse(Node nodeFormat) {
		Element elementFormat = (Element) nodeFormat;
		if (elementFormat.hasChildNodes()) {
			final ActionBarSpec actionBarSpec = new ActionBarSpec();
			NodeList nodeListFormat = elementFormat
			        .getElementsByTagName(ACTION_BAR);
			if (nodeListFormat.getLength() == 0) {
				return null;
			}

			nodeFormat = nodeListFormat.item(0);
			if (nodeFormat.hasAttributes()) {
				actionBarSpec.setControlId(XmlPaserUtil.getAttrValue(
				        nodeFormat, ID));
				String value = XmlPaserUtil.getAttrValue(nodeFormat, TITLE);
				if (!TextUtils.isEmpty(value)) {
					actionBarSpec.setTitle(ResourcesHelper
					        .getStringByName(value));
				}

				value = XmlPaserUtil.getAttrValue(nodeFormat, SUB_TITLE);
				if (!TextUtils.isEmpty(value)) {
					actionBarSpec.setSubTitle(ResourcesHelper
					        .getStringByName(value));
				}

				value = XmlPaserUtil.getAttrValue(nodeFormat, ICON);
				if (!TextUtils.isEmpty(value)) {
					actionBarSpec.setIcon(value);
				}

				value = XmlPaserUtil.getAttrValue(nodeFormat,
				        IS_DISPLAY_HOME_AS_UP);
				if (!TextUtils.isEmpty(value)) {
					if (value.equalsIgnoreCase("Y")) {
						actionBarSpec.setDisplayHomeAsUp(true);
					}
				}

				value = XmlPaserUtil.getAttrValue(nodeFormat,
				        IS_DISPLAY_SHOW_HOME_ENABLE);
				if (!TextUtils.isEmpty(value)) {
					if (value.equalsIgnoreCase("Y")) {
						actionBarSpec.setDisplayShowHomeEnabled(true);
					}
				}

				value = XmlPaserUtil.getAttrValue(nodeFormat,
				        IS_DISPLAY_SHOW_TITLE_ENABLE);
				if (!TextUtils.isEmpty(value)) {
					if (value.equalsIgnoreCase("N")) {
						actionBarSpec.setDisplayShowTitleEnabled(false);
					}
				}
			}

			if (nodeFormat.getNodeType() == Node.ELEMENT_NODE) {
				elementFormat = (Element) nodeFormat;
				nodeListFormat = elementFormat
				        .getElementsByTagName(SEARCH_VIEW);
				if (nodeListFormat.getLength() == 1) {
					final SearchViewSpec searchViewSpec = parseSearchView(nodeListFormat
					        .item(0));
					actionBarSpec.setSearchViewSpec(searchViewSpec);
				}
				nodeListFormat = elementFormat.getElementsByTagName(SELECTION);
				if (nodeListFormat.getLength() == 1) {
					final SelectionSpec selectionSpec = parseSelectionSpec(nodeListFormat
					        .item(0));
					actionBarSpec.setSelectionSpec(selectionSpec);
				}
				nodeListFormat = elementFormat
				        .getElementsByTagName(MenuConstant.SUB_MENU);
				for (int i = 0; i < nodeListFormat.getLength(); i++) {
					final Node nodeItem = nodeListFormat.item(i);
					final MenuItemSpec popupMenuItem = MenuItemParser
					        .parseMenuItemSpec(nodeItem);
					actionBarSpec.getOptionMenus().add(popupMenuItem);
				}

				nodeListFormat = elementFormat.getElementsByTagName(BUTTONS);
				if (nodeListFormat.getLength() == 1) {
					nodeListFormat = elementFormat.getElementsByTagName(BUTTON);
					for (int i = 0; i < nodeListFormat.getLength(); i++) {
						final Node nodeItem = nodeListFormat.item(i);
						final MenuItemSpec popupMenuItem = MenuItemParser
						        .parseMenuItemSpec(nodeItem);
						actionBarSpec.getActionBarButtons().add(popupMenuItem);
					}
				}
			}
			return actionBarSpec;
		}
		return null;
	}

	private SearchViewSpec parseSearchView(final Node node) {
		final SearchViewSpec searchViewSpec = new SearchViewSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node, ID);
			if (!TextUtils.isEmpty(value)) {
				searchViewSpec.setID(value);
			}

			value = XmlPaserUtil.getAttrValue(node, TITLE);
			if (!TextUtils.isEmpty(value)) {
				searchViewSpec.setTitle(value);
			}

			value = XmlPaserUtil.getAttrValue(node, ICON);
			if (!TextUtils.isEmpty(value)) {
				searchViewSpec.setIcon(value);
			}
		}

		final NodeList nodeList = ((Element) node).getElementsByTagName(FIELD);
		Node field = null;
		String value = null;
		for (int i = 0; i < nodeList.getLength(); i++) {
			field = nodeList.item(i);
			value = XmlPaserUtil.getNodeValue(field);
			if (!TextUtils.isEmpty(value)) {
				searchViewSpec.addSearchField(value);
			}
		}

		return searchViewSpec;
	}

	private SelectionSpec parseSelectionSpec(final Node node) {
		final SelectionSpec selectionSpec = new SelectionSpec();
		if (node.hasAttributes()) {
			String value = XmlPaserUtil.getAttrValue(node, CONTENT_DATA);
			if (!TextUtils.isEmpty(value)) {
				selectionSpec.setContentData(value);
			}

			value = XmlPaserUtil.getAttrValue(node, SEARCH_FOR);
			if (!TextUtils.isEmpty(value)) {
				selectionSpec.setSearchFor(value);
			}

			value = XmlPaserUtil.getAttrValue(node, DISPLAY_PROPERTY);
			if (!TextUtils.isEmpty(value)) {
				selectionSpec.setDisplayProperty(value);
			}

			value = XmlPaserUtil.getAttrValue(node, VALUE_PROPERTY);
			if (!TextUtils.isEmpty(value)) {
				selectionSpec.setValueProperty(value);
			}
			value = XmlPaserUtil.getAttrValue(node, IGNORE_INDEX);
			if (!TextUtils.isEmpty(value)) {
				selectionSpec.setIgnoreIndex(Integer.parseInt(value));
			}
		}

		final NodeList nodeList = ((Element) node).getElementsByTagName(FIELD);
		Node field = null;
		String value = null;
		for (int i = 0; i < nodeList.getLength(); i++) {
			field = nodeList.item(i);
			value = XmlPaserUtil.getNodeValue(field);
			if (!TextUtils.isEmpty(value)) {
				selectionSpec.addFilterField(value);
			}
		}
		return selectionSpec;
	}
}
